Q-3: SeekBar in Android OR LinearLayout Usage (5 Marks)
Questions​
a) What is the use seekbar in Android? Discuss various methods associated with seekbar.
OR
b) How do we use linear layout in android application? Explain with code.
Answers​
a) SeekBar in Android - Usage and Methods​
Definition and Purpose​
SeekBar is an Android UI widget that allows users to select a value from a continuous range by dragging a thumb along a horizontal track.
Primary Uses​
- Volume Control: Audio/video volume adjustment
- Brightness Control: Screen brightness settings
- Progress Selection: Media playback position
- Range Selection: Price ranges, age selection
- Settings Adjustment: Various app settings
- Rating Systems: User rating input
Basic SeekBar Implementation​
XML Layout:
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:layout_margin="16dp" />
<TextView
android:id="@+id/textViewProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Progress: 50"
android:layout_gravity="center" />
Key Methods and Properties​
1. Progress Management Methods
SeekBar seekBar = findViewById(R.id.seekBar);
// Set maximum value
seekBar.setMax(100);
// Set current progress
seekBar.setProgress(25);
// Get current progress
int currentProgress = seekBar.getProgress();
// Set secondary progress (for buffering indication)
seekBar.setSecondaryProgress(75);
// Get secondary progress
int secondaryProgress = seekBar.getSecondaryProgress();
// Increment progress by specific value
seekBar.incrementProgressBy(10);
// Increment secondary progress
seekBar.incrementSecondaryProgressBy(5);
2. Listener Methods
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// Called when progress changes
TextView textView = findViewById(R.id.textViewProgress);
textView.setText("Progress: " + progress);
// Check if change was initiated by user
if (fromUser) {
Log.d("SeekBar", "User changed progress to: " + progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Called when user starts dragging the thumb
Log.d("SeekBar", "Started tracking touch");
// Pause auto-updates or other operations
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Called when user stops dragging the thumb
Log.d("SeekBar", "Stopped tracking touch");
// Resume operations or save final value
}
});
3. Appearance and Behavior Methods
// Enable/disable user interaction
seekBar.setEnabled(true);
seekBar.setEnabled(false);
// Set thumb drawable
seekBar.setThumb(ContextCompat.getDrawable(this, R.drawable.custom_thumb));
// Set progress drawable
seekBar.setProgressDrawable(ContextCompat.getDrawable(this, R.drawable.custom_progress));
// Set thumb offset
seekBar.setThumbOffset(10);
// Check if SeekBar is enabled
boolean isEnabled = seekBar.isEnabled();
Complete SeekBar Example​
public class SeekBarActivity extends AppCompatActivity {
private SeekBar volumeSeekBar, brightnessSeekBar;
private TextView volumeText, brightnessText;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seekbar);
initializeViews();
setupSeekBars();
setupMediaPlayer();
}
private void initializeViews() {
volumeSeekBar = findViewById(R.id.volumeSeekBar);
brightnessSeekBar = findViewById(R.id.brightnessSeekBar);
volumeText = findViewById(R.id.volumeText);
brightnessText = findViewById(R.id.brightnessText);
}
private void setupSeekBars() {
// Volume SeekBar
volumeSeekBar.setMax(100);
volumeSeekBar.setProgress(50);
volumeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
volumeText.setText("Volume: " + progress + "%");
if (mediaPlayer != null && fromUser) {
float volume = progress / 100.0f;
mediaPlayer.setVolume(volume, volume);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Optional: Show volume indicator
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Optional: Save volume preference
saveVolumePreference(seekBar.getProgress());
}
});
// Brightness SeekBar
brightnessSeekBar.setMax(255);
brightnessSeekBar.setProgress(128);
brightnessSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
brightnessText.setText("Brightness: " + (progress * 100 / 255) + "%");
if (fromUser) {
adjustScreenBrightness(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
saveBrightnessPreference(seekBar.getProgress());
}
});
}
private void setupMediaPlayer() {
mediaPlayer = MediaPlayer.create(this, R.raw.sample_audio);
mediaPlayer.setLooping(true);
}
private void adjustScreenBrightness(int brightness) {
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = brightness / 255.0f;
getWindow().setAttributes(layoutParams);
}
private void saveVolumePreference(int volume) {
SharedPreferences prefs = getSharedPreferences("settings", MODE_PRIVATE);
prefs.edit().putInt("volume", volume).apply();
}
private void saveBrightnessPreference(int brightness) {
SharedPreferences prefs = getSharedPreferences("settings", MODE_PRIVATE);
prefs.edit().putInt("brightness", brightness).apply();
}
}
Advanced SeekBar Features​
Custom SeekBar with Range Selection:
public class RangeSeekBar extends View {
private int minValue = 0;
private int maxValue = 100;
private int minThumbValue = 20;
private int maxThumbValue = 80;
// Custom implementation for range selection
// Useful for price ranges, age ranges, etc.
}
Vertical SeekBar:
<SeekBar
android:id="@+id/verticalSeekBar"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:rotation="270"
android:transformPivotX="50dp"
android:transformPivotY="50dp" />
b) LinearLayout Usage in Android Applications​
Definition​
LinearLayout is a ViewGroup that arranges its child views in a single direction, either horizontally or vertically.
Key Characteristics​
- Sequential Arrangement: Children arranged in sequence
- Orientation: Horizontal or vertical orientation
- Weight Distribution: Proportional space allocation
- Gravity: Alignment control
- Simple Layout: Easy to understand and implement
Basic LinearLayout Structure​
Vertical LinearLayout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="24sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginBottom="16dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name"
android:layout_marginBottom="12dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
Horizontal LinearLayout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_margin="16dp">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"
android:layout_marginEnd="8dp" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK"
android:layout_marginStart="8dp" />
</LinearLayout>
Important Attributes​
1. Orientation
android:orientation="vertical" <!-- Stack vertically -->
android:orientation="horizontal" <!-- Arrange horizontally -->
2. Layout Weight
android:layout_weight="1" <!-- Proportional space allocation -->
android:layout_width="0dp" <!-- Use with weight for flexible width -->
3. Gravity
android:gravity="center" <!-- Center children -->
android:gravity="center_vertical" <!-- Center vertically -->
android:gravity="start|center_vertical" <!-- Left align + center vertically -->
Complete LinearLayout Example​
activity_linear_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:background="#F5F5F5">
<!-- Header Section -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="#2196F3"
android:padding="16dp"
android:layout_marginBottom="16dp">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/ic_user"
android:layout_marginEnd="16dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Profile"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Manage your account"
android:textColor="@android:color/white"
android:textSize="14sp" />
</LinearLayout>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/ic_settings"
android:layout_marginStart="16dp" />
</LinearLayout>
<!-- Form Section -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/white"
android:padding="16dp"
android:layout_marginBottom="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Personal Information"
android:textSize="16sp"
android:textStyle="bold"
android:layout_marginBottom="16dp" />
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Full Name"
android:layout_marginBottom="12dp" />
<EditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email Address"
android:inputType="textEmailAddress"
android:layout_marginBottom="12dp" />
<EditText
android:id="@+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:inputType="phone"
android:layout_marginBottom="16dp" />
<!-- Horizontal buttons -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="end">
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:style="?android:attr/buttonStyleSmall"
android:layout_marginEnd="8dp" />
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:style="?android:attr/buttonStyleSmall" />
</LinearLayout>
</LinearLayout>
<!-- Statistics Section with Weight Distribution -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@android:color/white"
android:padding="16dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="125"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#4CAF50" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Posts"
android:textSize="12sp" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#E0E0E0" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1.2K"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#2196F3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Followers"
android:textSize="12sp" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#E0E0E0" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="890"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#FF9800" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Following"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity.java:
public class LinearLayoutActivity extends AppCompatActivity {
private EditText editTextName, editTextEmail, editTextPhone;
private Button btnSave, btnCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linear_layout);
initializeViews();
setupClickListeners();
}
private void initializeViews() {
editTextName = findViewById(R.id.editTextName);
editTextEmail = findViewById(R.id.editTextEmail);
editTextPhone = findViewById(R.id.editTextPhone);
btnSave = findViewById(R.id.btnSave);
btnCancel = findViewById(R.id.btnCancel);
}
private void setupClickListeners() {
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveUserData();
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearFields();
}
});
}
private void saveUserData() {
String name = editTextName.getText().toString().trim();
String email = editTextEmail.getText().toString().trim();
String phone = editTextPhone.getText().toString().trim();
if (validateInput(name, email, phone)) {
// Save data logic
Toast.makeText(this, "Data saved successfully", Toast.LENGTH_SHORT).show();
}
}
private boolean validateInput(String name, String email, String phone) {
if (name.isEmpty()) {
editTextName.setError("Name is required");
return false;
}
if (email.isEmpty()) {
editTextEmail.setError("Email is required");
return false;
}
return true;
}
private void clearFields() {
editTextName.setText("");
editTextEmail.setText("");
editTextPhone.setText("");
}
}
LinearLayout Advantages​
- Simple to Use: Easy to understand and implement
- Predictable Layout: Clear visual hierarchy
- Weight Distribution: Flexible space allocation
- Performance: Good performance for simple layouts
- Nested Layouts: Can be nested for complex designs
LinearLayout Best Practices​
- Avoid Deep Nesting: Use ConstraintLayout for complex layouts
- Use Weights Wisely: For proportional distribution
- Consider Orientation: Choose appropriate orientation
- Optimize Performance: Minimize layout hierarchy depth